# -*- coding: utf-8 -*-
"""
@Author: CPS
@Date: 2020-08-07 17:11:05
@Last Modified by: CPS
@Last Modified time: 2020-08-13 17:16:12
@file_path D:\CPS\MyProject\Electron-fileWatcher\src\python\iconFilter\main.py
@Filename D:/CPS/MyProject/Electron-fileWatcher/src/python/icon.py/icon.py
"""

import sys
import os
import xml.dom.minidom
sys.path.append(os.path.join(os.getcwd()))
import iconFilter

##
# @brief      将指定目录下面的 svg文件进行批量输出
#
##
# @param      inputDir   The input dir
# @param      outputDir  The output dir
# @param      options    The options
##
# @return     { description_of_the_return_value }
##


def main(inputDir='./', outputDir=0, options={
    'target': ['.svg'],
    'svgPath': './',
}):
    inputDir = os.path.abspath(inputDir)
    if not os.access(inputDir, os.F_OK):
        return print('请检查文件是否存在')

    # 生成输出目录
    if outputDir == 0:
        outputDir = inputDir
    else:
        if not os.access(outputDir, os.F_OK):
            os.mkdir(outputDir)

    fileList = os.listdir(inputDir)
    if not len(fileList) > 0:
        return

    res = {
        'src': [],
        'svg': [],
    }

    for each in fileList:
        name, ext = os.path.splitext(each)
        for eachTar in options['target']:
            if ext == eachTar:
                res['src'].append("import src{0} from '!!file-loader!{2}{1}';".format(name, each, options['svgPath']))
                res['svg'].append("import svg{0} from '!!svg-inline-loader!{2}{1}';".format(name, each, options['svgPath']))

    # 保存文件
    f = open(os.path.join(outputDir, 'icon.js'), 'w', encoding='utf-8')
    f.write('''
//create by createIconJS.py
//Author:CPS

//src的目录前缀
function prefix(t) {
  return 'renderer/' + t
}\n\n''')
    for x in range(0, len(res['src'])):
        f.write(res['src'][x])
        f.write('\n')
        f.write(res['svg'][x])
        f.write('\n\n')

    f.write('\n\n')
    f.write('export default {\n')
    f.write('  src: {\n')
    for each in fileList:
        name, ext = os.path.splitext(each)
        if not ext in options['target']:
            continue
        f.write('    {name}: prefix(src{name}),\n'.format(name=name))
    f.write('  },\n')
    f.write('  svg: {\n')
    for each in fileList:
        name, ext = os.path.splitext(each)
        if not ext in options['target']:
            continue
        f.write('    {name}: svg{name},\n'.format(name=name))

    f.write('  }\n')
    f.write('}\n')
    f.close()


if (__name__ == "__main__"):
    # 默认处理当前目录下的 input
    tar = 'D:/CPS/MyProject/Electron-fileWatcher/src/renderer/pages/fileWatcher/js/config/icon/old'
    output = 'D:/CPS/MyProject/Electron-fileWatcher/src/renderer/pages/fileWatcher/js/config/icon'
    # 转换为绝对路径
    tar = os.path.abspath(tar)

    # 可通过外部参数进行
    if len(sys.argv) > 1:
        print('当前输入是%s' % sys.argv[1])
        tar = sys.argv[1]

    iconFilter.main(tar, output)
    main(output, output)
